package view.model;

import java.util.ArrayList;

import javax.swing.ComboBoxModel;
import javax.swing.event.ListDataListener;

import entity.BookType;
import service.book.BookTypeServer;
import service.book.BookTypeServerImpl;
import utils.ComonUtil;


/**
 * @Author: Lin Hongwei
 * @LastEditors: Lin Hongwei
 * @email: 1365755396@qq.com
 * @CSDN: https://blog.csdn.net/weixin_45429409
 * @Description: 下拉框选项模板
 * - 下拉框数据填充的一种，另一种通过setItem直接添加
 *
 */
public class BookTypeComboBoxModel implements ComboBoxModel<BookType>{
	private static BookTypeServer bookTypeServer = new BookTypeServerImpl();
	public static ArrayList<BookType> typeList = (ArrayList<BookType>) bookTypeServer.getBookTypeList(null);
	BookType type;
	
	/**
     * 通过数组内容查找下标(由于getSelectedItem返回的是BookType类型，所以在set直接set文本的时候不生效)
     * @param bookType
     * @return
     */
    public int indexOf(String bookTypeName) {
        if(ComonUtil.isEmpty(bookTypeName)) {
            return -1;
        } else {
            for(int i = 0; i < typeList.size(); i++) {
				//                if(bookType.getBookTypeName() == typeList.get(i).getBookTypeName()) {
				//                    return i;
				//                }
            	 if(typeList.get(i).getBookTypeName().equals(bookTypeName)) {
                     return i;
                 }
            }
        }
        return -1;
    }

	
	/**
	 * @ 返回所选选项
	 */
	@Override
	public BookType getSelectedItem() {
		// TODO Auto-generated method stub
		if(typeList!=null)return type;
		return null;
	}
	
	/**
	 * @ 设置所选项目
	 */
	@Override
	public void setSelectedItem(Object arg0) {
		// TODO Auto-generated method stub
		type = (BookType) arg0;
	}
	
	

	@Override
	public int getSize() {
		// TODO Auto-generated method stub
		return typeList.size();
	}
	
	
	/**
	 * @ 获取到对应位置的元素
	 */
	@Override
	public BookType getElementAt(int index) {
		// TODO Auto-generated method stub
		return typeList.get(index);
	}

	/**
	 * @ 事件监听
	 */
	@Override
	public void addListDataListener(ListDataListener arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void removeListDataListener(ListDataListener arg0) {
		// TODO Auto-generated method stub
		
	}
	

}
